package com.kly.juc.atomic;

import lombok.Getter;

import java.util.concurrent.atomic.*;

/**
 * @author admin
 * @date 2025/4/17
 */
public class LongAccumulatorDemo {

}

@Getter
class MyBook {
    private long price = 0L;

    /**
     * synchronized
     */
    public synchronized void addPrice() {
        this.price++;
    }

    /**
     * AtomicLong
     */
    AtomicLong atomicLong = new AtomicLong();
    public void addPriceByAtomicLong() {
        atomicLong.incrementAndGet();
    }

    /**
     * LongAdder
     */
    LongAdder longAdder = new LongAdder();
    public void addPriceByLongAdder() {
        longAdder.increment();
    }

    /**
     * LongAccumulator
     */
    LongAccumulator longAccumulator = new LongAccumulator(Long::sum, 0);
    public void addPriceByLongAccumulator() {
        longAccumulator.accumulate(1);
    }

    DoubleAdder doubleAdder = new DoubleAdder();
    public void addPriceByDoubleAdder() {
        doubleAdder.add(1);
    }

    DoubleAccumulator doubleAccumulator = new DoubleAccumulator(Double::sum, 0);
    public void addPriceByDoubleAccumulator() {
        doubleAccumulator.accumulate(1);
    }

}